import { getUrl } from '@/utils/imagePlugin'
import { formatDate } from "@/utils/date";
export const formatDistance = (address: string, lat2: number, lon2: number) => {
	//  && (/^\{.*\}$/.test(address))
	if(address){
		let lat1 = JSON.parse(address).latitude
		let lon1 = JSON.parse(address).longitude
		let EARTH_RADIUS = 6378.137; // 地球半径
		let radLat1 = lat1 * Math.PI / 180.0; //lat1 * Math.PI / 180.0=>弧度计算
		let radLat2 = lat2 * Math.PI / 180.0;
		let a = radLat1 - radLat2;
		let b = lon1 * Math.PI / 180.0 - lon2 * Math.PI / 180.0;
		let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) *
				Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		if(s > 1){
			s = Math.round(s);
			return s + 'km'
		} else {
			s = Math.round(s * 1000)
			return s + 'm'
		}
	} else {
		return '位置未知'
	}
}

export const formatDetailedAddress = (address: string) => {
	//  && (/^\{.*\}$/.test(address))
	if(address){
		return JSON.parse(address).address
	} else {
		return address
	}
}

export const formatTime = (date: any, format: string) => {
	if(date){
		return formatDate(new Date(Number(date)), format)
	} else {
		return '暂无'
	}
}


/**
 * 时间间隔函数
 *  1、3分钟内发布的显示：刚刚
 *  2、3-60分钟发布的显示：XX分钟前
 *  3、24小时以下发布的显示：XX小时前   
 * @function timeInterval
 * @param { String || number } params.date  目标时间  "yyyy-mm-dd hh:ss" || "yyyy/mm/dd hh:ss" || 1737514571575 
 * @param { Number } params.n     结果超出多少天，返回目标时间，默认超出1天
 * @returns { String }  
 */
type timeIntervalParams = {
	date: string,
	n?: number,
}
export const timeInterval = (params: timeIntervalParams):string =>  {
	
	if (!params)  return "";
   
	if (typeof params != 'object') {
		params = {
			date: params
		}
	}
	
	const { date, n = 1 } = params 	
	
	const curTime = new Date()
	const postTime = new Date(date)

	const timeDiff = curTime.getTime() - postTime.getTime();

	const min = 60 * 1000,
		hour = min * 60,
		day = hour * 24,			
		exceedDay = Math.floor(timeDiff / day),
		exceedHour = Math.floor(timeDiff / hour),
		exceedMin = Math.floor(timeDiff / min);

		console.log('exceedDay')
		console.log(exceedDay)
	
	if (exceedDay >= n) 
		return typeof date == 'number' ? formatTime(date,'yyyy-MM-dd hh:mm:ss') : date;
	else 
		if (exceedDay < n && exceedDay > 0) 
			return exceedDay + '天前';
		else 
			if (exceedHour < 24 && exceedHour > 0) 
				return exceedHour + '小时前';
			else 
				if (exceedMin < 3) 
					return '刚刚'
				else 
					return exceedMin + '分种前';	

}
